﻿using Janus.BaseEntitys;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Janus.Cores.Extension
{
    public static class EntityExtension
    {
        /// <summary>
        /// 转换对象
        /// </summary>
        /// <typeparam name="DTO"></typeparam>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="dto"></param>
        /// <returns></returns>
        public static T ToEntity<T>(this Entity dto)
            where T : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<T>(dto);
            return poco;
        }

        public static List<T> ToEntitys<T>(this IEnumerable<Entity> dto)
        where T : Entity
        {
            if (dto.HasValues())
            {
                var poco = AutoMapper.Mapper.DynamicMap<List<T>>(dto);
                return poco;
            }
            else
            {
                return new List<T>();
            }
        }
        public static SearchEntity<POCO> ToPOCO<DTO, POCO>(this SearchEntity<DTO> searchEntity)
            where DTO : Entity, new()
            where POCO : Entity, new()
        {
            SearchEntity<POCO> result = new SearchEntity<POCO>(null, null);
            var poco = AutoMapper.Mapper.DynamicMap<POCO>(searchEntity.Model);
            result.Model = poco;
            result.CurrentUser = searchEntity.CurrentUser;
            result.IsAdmin = searchEntity.IsAdmin;
            result.Pagination = searchEntity.Pagination;
            result.Queriers = searchEntity.Queriers;

            return result;
        }

        public static Hashtable ToHashtable(this Entity model)
        {
            Hashtable table = new Hashtable();
            var type = model.GetType();
            foreach (var p in type.GetProperties())
            {
                var val = p.GetValue(model, null);
                if (val != null)
                {
                    table.Add(p.Name, val);
                }
            }
            return table;
        }

        /// <summary>
        /// hashTable 转换为Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashtable"></param>
        /// <returns></returns>
        public static T ToEntity<T>(this Hashtable hashtable) where T : new()
        {
            T t = new T();
            foreach (var item in t.GetType().GetProperties())
            {
                if (hashtable.ContainsKey(item.Name))
                {
                    item.SetValue(t, hashtable[item.Name], null);
                }
            }
            return t;
        }

        /// <summary>
        /// hashTable 转换为Entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashtable"></param>
        /// <returns></returns>
        public static List<T> ToEntity<T>(this IList<Hashtable> hashtable) where T : new()
        {
            List<T> t = new List<T>();
            foreach (var item in hashtable)
            {
                var result = item.ToEntity<T>();
                t.Add(result);
            }

            return t;
        }

        public static bool IsNotEmpty(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }
        public static bool HasValues(this IEnumerable<object> list)
        {
            return list != null && list.Count() > 0;
        }
    }
}
